package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrCodeBean;
import com.koron.css2.baseConfig.bean.UseMatrTemplateBean;
import com.koron.css2.baseConfig.bean.UseMatrTemplateListBean;
import com.koron.css2.baseConfig.mapper.UseMatrTemplateMapper;
import com.koron.css2.baseConfig.queryBean.UseMatrTemplateQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.MatrCodeMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 用料模板-编辑初始化
 *
 * @author 廖大富
 */
public class UseMatrTemplateQuery implements ServerInterface {

    private static Logger logger = LogManager.getLogger(UseMatrTemplateQuery.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<UseMatrTemplateBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", UseMatrTemplateBean.class);
        try {
            UseMatrTemplateQueryBean bean = JsonUtils.objectToPojo(req.getData(), UseMatrTemplateQueryBean.class);
            MatrCodeMapper matrMapper = factory.getMapper(MatrCodeMapper.class, "_default");
            UseMatrTemplateMapper mapper = factory.getMapper(UseMatrTemplateMapper.class);
            if (StringUtils.isEmpty(bean.getTemplateId())){
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "模板id不能为空", void.class);
            }
            // 查询用料模板信息
            UseMatrTemplateBean useMatrTemplateBean = mapper.getMatrTemplate(bean.getTemplateId());
            // 查询用料模板明细信息
            List<UseMatrTemplateListBean> useMatrTemplateList = mapper.getMatrTemplateListByTemplateId(bean.getTemplateId());
            // 获取材料信息
            for (UseMatrTemplateListBean useMatrTemplateListBean : useMatrTemplateList) {
                MatrCodeBean matrCodeBean = matrMapper.selectMatrTemplateByNo(useMatrTemplateListBean.getMatrNo());
                if(matrCodeBean != null) {
                    useMatrTemplateListBean.setMatrId(matrCodeBean.getId());
                    useMatrTemplateListBean.setMatrName(matrCodeBean.getMatrName());
                    useMatrTemplateListBean.setMatrMode(matrCodeBean.getMatrMode());
                    useMatrTemplateListBean.setMatrUnit(matrCodeBean.getMatrUnit());
                }
            }
            useMatrTemplateBean.setUseMatrTemplateList(useMatrTemplateList);
            info.setData(useMatrTemplateBean);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }
}
